﻿Imports System.Drawing.Drawing2D

Public Class DefaultTheme : Implements Theme

	Sub New()
		If Bitmaps(0) Is Nothing Then PrepareBitmaps()
	End Sub

	Public Sub Apply(sender As Object) Implements Theme.Apply
		If TypeOf sender Is Form Then
			sender.Font = Font
		ElseIf TypeOf sender Is Homepage Then
			'sender.BackgroundImage = Bitmaps(0)
			'sender.BackgroundImageLayout = ImageLayout.Stretch
			sender.Font = Font
		ElseIf TypeOf sender Is TabStrip Then
			sender.Font = Font
			sender.BackgroundImage = My.Resources.panelback
			sender.BackgroundImageLayout = ImageLayout.Tile
		ElseIf TypeOf sender Is LeftBar Then
			sender.Font = Font
			sender.BackColor = FormColor
		ElseIf TypeOf sender Is Filter Then
			sender.Font = Font
			sender.BackColor = FormColor
		ElseIf TypeOf sender Is Window Then
			sender.BackColor = FormColor
			sender.Font = Font
		ElseIf TypeOf sender Is Page Then
			sender.BackColor = FormColor
			sender.Font = Font
		ElseIf TypeOf sender Is Control Then
			sender.BackColor = FormColor
			sender.Font = Font
		End If
	End Sub

	Public ReadOnly Property Font As System.Drawing.Font Implements Theme.Font
		Get
			Return New Font("segoe ui", 9)
		End Get
	End Property

	Public Sub Paint(g As System.Drawing.Graphics, sender As Object) Implements Theme.PaintForm
		Dim R As Rectangle
		Static i As Integer = 0
		i += 1
		g.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
		g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
		For Each Control As Object In sender.Controls
			If TypeOf Control Is BaseControl Then
				If Not TypeOf Control Is CheckBox And Not TypeOf Control Is Button Then
					R = Control.Bounds
					R.Width -= 1 : R.Height -= 1
					g.FillRectangle(CType(Control, Control).BackColor.Brush, R)
					R.Inflate(3, 3)
					For i = 1 To 2
						DrawRoundRect(g, R, New Pen(Color.FromArgb(64 / i, Color.Black), i), 4 + i)
						R.Inflate(1, 1)
					Next i
				End If
			ElseIf TypeOf Control Is Label And Not TypeOf sender Is HtmlLayout Then
				Control.Visible = False
				If Control.Enabled Then
					Dim F As New StringFormat
					g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
					F.Alignment = StringAlignment.Near
					Dim Label As Label = Control
					Dim Tw As Integer = g.MeasureString(Label.Text, Label.Font).Width + 0.5
					R = Label.Bounds
					R.Inflate(-2, 0)
					Select Case Label.TextAlign
						Case ContentAlignment.TopLeft, ContentAlignment.MiddleLeft, ContentAlignment.BottomLeft : R = New Rectangle(R.Left, R.Top, Tw, R.Height) : F.Alignment = StringAlignment.Near
						Case ContentAlignment.TopCenter, ContentAlignment.MiddleCenter, ContentAlignment.BottomCenter : R.Offset((R.Width - Tw) / 2, 0)
						Case ContentAlignment.TopRight, ContentAlignment.MiddleRight, ContentAlignment.BottomRight : R = New Rectangle(R.Right - Tw * 2, R.Top, Tw * 2, R.Height) : F.Alignment = StringAlignment.Far
					End Select
					F.LineAlignment = StringAlignment.Center
					DrawText2(g, Label.Text, Label.Font, LabelColor, R, F)
				End If
			End If
		Next
	End Sub

	Public Sub PaintListHeader(g As System.Drawing.Graphics, R As System.Drawing.Rectangle) Implements Theme.PaintListHeader
		g.SmoothingMode = Drawing2D.SmoothingMode.None
		g.FillRectangle(New SolidBrush(FormColor), R)
		g.DrawLine(New Pen(Color.FromArgb(32, Color.Black)), R.Right - 1, R.Top + 3, R.Right - 1, R.Bottom - 4)
		g.DrawLine(New Pen(Color.FromArgb(16, Color.Black)), R.Left, R.Bottom - 1, R.Right - 1, R.Bottom - 1)
	End Sub

	Public Sub PaintListColumnHeader(g As System.Drawing.Graphics, R As System.Drawing.Rectangle, Col As System.Windows.Forms.DataGridViewColumn, Filtered As Boolean) Implements Theme.PaintListColumnHeader
		PaintListHeader(g, R)
		g.SmoothingMode = Drawing2D.SmoothingMode.None
		If Col.Tag = "s" Then
			R.Inflate(-1, -1)
			R.Width -= 1 : R.Height -= 1
			g.FillRectangle(New SolidBrush(Color.FromArgb(48, Color.White)), R)
			R.Width -= 1 : R.Height -= 1
			g.DrawRectangle(New Pen(Color.FromArgb(64, Color.Black)), R)
			R.Width += 2 : R.Height += 2
			R.Inflate(1, 1)
		End If
		Dim SF As New StringFormat
		SF.LineAlignment = StringAlignment.Center
		Select Case Col.DefaultCellStyle.Alignment
			Case DataGridViewContentAlignment.MiddleLeft : SF.Alignment = StringAlignment.Near
			Case DataGridViewContentAlignment.MiddleCenter : SF.Alignment = StringAlignment.Center
			Case DataGridViewContentAlignment.MiddleRight : SF.Alignment = StringAlignment.Far
		End Select
		R.Inflate(-2, -2)
		g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
		If Filtered Then
			g.SmoothingMode = SmoothingMode.HighQuality
			g.PixelOffsetMode = PixelOffsetMode.HighQuality
			Dim RA = R
			RA.Offset(RA.Width - RA.Height + 2, 0)
			RA.Width = RA.Height - 4
			R.Width -= RA.Width + 2
			g.FillRoundRect(RA, Color.White.Opacity(192).Brush, 2)
			DrawStringLighted(g, "6", Marlett, Color.Black, RA, StrFormat("cm"))
			g.SmoothingMode = SmoothingMode.None
			g.PixelOffsetMode = PixelOffsetMode.None
		End If
		DrawStringLighted(g, Col.HeaderText, Font, Color.Black, R, SF)
	End Sub

	Public ReadOnly Property ReadOnlyColor As System.Drawing.Color Implements Theme.ReadOnlyColor
		Get
			Return Mix(FormColor, Color.White, 20)
		End Get
	End Property

	Public ReadOnly Property FormColor As System.Drawing.Color Implements Theme.FormColor
		Get
			Return Color.FromArgb(200, 208, 200)
		End Get
	End Property

	Public Sub Blur(G As System.Drawing.Graphics, Control As System.Windows.Forms.Control, Rect As System.Drawing.Rectangle) Implements Theme.Blur
		G.Clip = New Region(Rect)
		G.InterpolationMode = InterpolationMode.HighQualityBilinear
		If Control.Width / Control.Height <= Bitmaps(1).Width / Bitmaps(1).Height Then
			Dim h = Control.Height
			Dim w = Bitmaps(1).Width / Bitmaps(1).Height * h
			Dim R As New Rectangle(-(w - Control.Width) / 2, 0, w, h)
			G.DrawImage(Bitmaps(1), R)
		Else
			Dim w = Control.Width
			Dim h = Bitmaps(1).Height / Bitmaps(1).Width * w
			Dim R As New Rectangle(0, -(h - Control.Height) / 2, w, h)
			G.DrawImage(Bitmaps(1), R)
		End If
		G.ResetClip()
	End Sub

	Public Sub PaintListRowHeader(g As System.Drawing.Graphics, R As System.Drawing.Rectangle, Row As System.Windows.Forms.DataGridViewRow) Implements Theme.PaintListRowHeader
		PaintListHeader(g, R)
		g.SmoothingMode = Drawing2D.SmoothingMode.None
		If Row.Tag = "s" Then
			R.Inflate(-1, -1)
			R.Width -= 1 : R.Height -= 1
			g.FillRectangle(New SolidBrush(Color.FromArgb(48, Color.White)), R)
			R.Width -= 1 : R.Height -= 1
			g.DrawRectangle(New Pen(Color.FromArgb(64, Color.Black)), R)
			R.Width += 2 : R.Height += 2
			R.Inflate(1, 1)
		End If
		Dim SF As New StringFormat
		SF.LineAlignment = StringAlignment.Center
		Select Case Row.DefaultCellStyle.Alignment
			Case DataGridViewContentAlignment.MiddleLeft : SF.Alignment = StringAlignment.Near
			Case DataGridViewContentAlignment.MiddleCenter : SF.Alignment = StringAlignment.Center
			Case DataGridViewContentAlignment.MiddleRight : SF.Alignment = StringAlignment.Far
		End Select
		R.Inflate(-2, -2)
		g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
		DrawStringLighted(g, Row.HeaderCell.Value, Font, Color.Black, R, SF)
	End Sub

	Public ReadOnly Property LabelColor As System.Drawing.Color Implements Theme.LabelColor
		Get
			Return Mix(Color.Black, FormColor, 40)
		End Get
	End Property
End Class
